<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-header">
        <div class="logo">
          <div class="logo-text">MMC</div>
        </div>
        <h1 class="title">MMC 后台管理系统</h1>
        <p class="subtitle">现代化的后台管理解决方案</p>
      </div>

      <a-form
        ref="formRef"
        :model="loginForm"
        :rules="rules"
        class="login-form"
        size="large"
        @submit="handleLogin"
      >
        <a-form-item field="username" hide-label>
          <a-input
            v-model="loginForm.username"
            placeholder="请输入用户名"
            allow-clear
          >
            <template #prefix>
              <icon-user />
            </template>
          </a-input>
        </a-form-item>

        <a-form-item field="password" hide-label>
          <a-input-password
            v-model="loginForm.password"
            placeholder="请输入密码"
            allow-clear
          >
            <template #prefix>
              <icon-lock />
            </template>
          </a-input-password>
        </a-form-item>

        <a-form-item v-if="captchaEnabled" field="captchaCode" hide-label>
          <div class="captcha-wrapper">
            <a-input
              v-model="loginForm.captchaCode"
              placeholder="请输入验证码"
              allow-clear
            >
              <template #prefix>
                <icon-safe />
              </template>
            </a-input>
            <div class="captcha-image" @click="refreshCaptcha">
              <img v-if="captchaImage" :src="captchaImage" alt="验证码" />
              <div v-else class="captcha-loading">
                <a-spin />
              </div>
            </div>
          </div>
        </a-form-item>

        <a-form-item hide-label>
          <div class="login-options">
            <a-checkbox v-model="loginForm.rememberMe">记住我</a-checkbox>
          </div>
        </a-form-item>

        <a-form-item hide-label>
          <a-button
            type="primary"
            html-type="submit"
            long
            :loading="loading"
            class="login-btn"
          >
            登录
          </a-button>
        </a-form-item>
      </a-form>

      <div class="login-footer">
        <p>© 2024 MMC. All rights reserved.</p>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/store/modules/user'
import { authApi } from '@/api/auth'
import { Message } from '@arco-design/web-vue'
import type { FormInstance } from '@arco-design/web-vue'

const router = useRouter()
const userStore = useUserStore()

const formRef = ref<FormInstance>()
const loading = ref(false)
const captchaEnabled = ref(true) // 默认启用验证码
const captchaImage = ref('')

// 登录表单
const loginForm = reactive({
  username: 'admin',
  password: 'admin123',
  captchaCode: '',
  captchaId: '',
  rememberMe: false
})

// 表单验证规则
const rules = {
  username: [
    { required: true, message: '请输入用户名' }
  ],
  password: [
    { required: true, message: '请输入密码' }
  ],
  captchaCode: [
    { required: captchaEnabled.value, message: '请输入验证码' }
  ]
}

// 获取验证码
const getCaptcha = async () => {
  try {
    const res = await authApi.getCaptcha()
    if (res.success && res.data) {
      captchaImage.value = res.data.captchaImage
      loginForm.captchaId = res.data.captchaId
      captchaEnabled.value = res.data.captchaEnabled
    } else {
      Message.error('获取验证码失败，请刷新页面重试')
      throw new Error('验证码获取失败')
    }
  } catch (error) {
    console.error('获取验证码失败:', error)
    Message.error('验证码服务异常，请刷新页面重试')
    // 保持验证码启用状态，不允许绕过验证码
    captchaEnabled.value = true
    captchaImage.value = ''
    loginForm.captchaId = ''
  }
}

// 刷新验证码
const refreshCaptcha = () => {
  loginForm.captchaCode = ''
  getCaptcha()
}

// 登录处理
const handleLogin = async ({ errors }: { errors: any }) => {
  if (errors) return

  // 验证码必填检查
  if (captchaEnabled.value && !loginForm.captchaCode) {
    Message.error('请完成验证码验证')
    return
  }

  loading.value = true
  try {
    await userStore.login(loginForm)
    Message.success('登录成功')
    router.push('/')
  } catch (error: any) {
    Message.error(error.message || '登录失败')
    // 登录失败后刷新验证码
    refreshCaptcha()
  } finally {
    loading.value = false
  }
}

onMounted(() => {
  // 首次加载时尝试获取验证码
  getCaptcha()
})
</script>

<style lang="scss" scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 20px;
}

.login-box {
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  border-radius: 16px;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
  padding: 40px;
  width: 100%;
  max-width: 400px;
}

.login-header {
  text-align: center;
  margin-bottom: 40px;

  .logo {
    margin-bottom: 16px;

    .logo-text {
      font-size: 32px;
      font-weight: 700;
      background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
      -webkit-background-clip: text;
      -webkit-text-fill-color: transparent;
      background-clip: text;
      letter-spacing: 2px;
      font-family: 'Arial', sans-serif;
    }
  }

  .title {
    font-size: 28px;
    font-weight: 600;
    color: #1d2129;
    margin-bottom: 8px;
  }

  .subtitle {
    color: #86909c;
    font-size: 14px;
    margin: 0;
  }
}

.login-form {
  .captcha-wrapper {
    display: flex;
    gap: 12px;

    .arco-input-wrapper {
      flex: 1;
    }

    .captcha-image {
      width: 100px;
      height: 32px;
      border: 1px solid #e5e6eb;
      border-radius: 6px;
      cursor: pointer;
      display: flex;
      align-items: center;
      justify-content: center;
      background: #f7f8fa;

      img {
        width: 100%;
        height: 100%;
        object-fit: cover;
        border-radius: 6px;
      }

      .captcha-loading {
        display: flex;
        align-items: center;
        justify-content: center;
        width: 100%;
        height: 100%;
      }

      &:hover {
        border-color: #165dff;
      }
    }
  }

  .login-options {
    display: flex;
    justify-content: space-between;
    align-items: center;
    width: 100%;
  }

  .login-btn {
    height: 40px;
    border-radius: 8px;
    font-weight: 500;
  }
}

.login-footer {
  text-align: center;
  margin-top: 32px;
  padding-top: 24px;
  border-top: 1px solid #e5e6eb;

  p {
    color: #86909c;
    font-size: 12px;
    margin: 0;
  }
}
</style> 